window.onload = function () {

//定义页面
var main = document.querySelector("#main");
//定义星星个数
var xing = document.querySelector(".xing");
let xing1 = document.querySelector("#xing1");
//console.log(xing1);
//定义篮球个数
var lan = document.querySelector(".qiu");
let qiu1 = document.querySelector("#qiu1");
//定义游戏失败
var end = document.querySelector("#end");
//定义星星分数
var elmscore2 = document.querySelector(".score2");
//定义篮球分数
var elmscore1 = document.querySelector(".score1");
//定义敌人数组
var enemyarry = [];
//定义星星数组
var xingarry = [];
//定义篮球数组
var qiuarry = [];
//定义切尔西数组
var qexarry = [];
//定义鸡肉数组
var jiarry = [];
//定义星星分数
let score2 = 10;
//定义篮球个数
let score1 = 0;
//封装随机生成数组
function getRandom(m, n) {
  return Math.floor(Math.random() * (n - m) + m);
}
//------------------------------------------------------------------------
//创造坤坤zhu进去
function myplane(imgsrc) {
  this.myplaneNode = document.createElement("img");
  //myplaneNode.src='image/D1.jpg';
  this.imgsrc = imgsrc;
  this.init = function () {
    this.myplaneNode.src = this.imgsrc;
    this.myplaneNode.style.position = "absolute";
    main.appendChild(this.myplaneNode);
    console.log(this.myplaneNode.style.width);
  };
  this.init();
  this.move = function () {
    setInterval(function () {
      //移动时执行X，Y赋值给坤坤
      document.onmousemove = function (e) {
        myplaneNode.style.width = 30 + score2 + "px";
        myplaneNode.style.height = 30 + score2 + "px";
        let l1 = main.offsetLeft;
        let t1 = main.offsetTop;
        let l = myplaneNode.clientWidth / 2;
        let t = myplaneNode.clientHeight / 2;
        let x1 = e.clientX;
        let y1 = e.clientY;
        x1 = e.clientX - l - l1;
        y1 = e.clientY - t - t1;
        if (x1 < -l) {
          x1 = -l;
        }
        myplaneNode.style.top = y1 + "px";
        myplaneNode.style.left = x1 + "px";
      };
    }, 50);
  };
  this.move();
}
myplane("image/zhu.jpg");

//----------------------------------------------------------------------------------
//添加小敌人
function Enemy(imgsrc, x, y) {
  this.imgNode = document.createElement("img");
  this.imgsrc = imgsrc;
  this.x = x;
  this.y = y;
  this.init = function () {
    this.imgNode.src = this.imgsrc;
    this.imgNode.style.position = "absolute";
    this.imgNode.style.left = this.x + "px";
    this.imgNode.style.top = this.y + "px";
    //加入敌人到节点下
    main.appendChild(this.imgNode);
  };
  this.init();
}
//创造敌人，向里面添加小黑子
function getEnemy() {
  let newEnemy1 = new Enemy(
    "image/D1.jpg",
    getRandom(0, 3000),
    getRandom(0, 1400)
  );
  let newEnemy2 = new Enemy(
    "image/D2.jpg",
    getRandom(0, 3000),
    getRandom(0, 1400)
  );
  let newEnemy3 = new Enemy(
    "image/D3.jpg",
    getRandom(0, 3000),
    getRandom(0, 1400)
  );
  let newEnemy4 = new Enemy(
    "image/D4.jpg",
    getRandom(0, 3000),
    getRandom(0, 1400)
  );
  let newEnemy5 = new Enemy(
    "image/D5.jpg",
    getRandom(0, 3000),
    getRandom(100, 1400)
  );
  //创建的敌方飞机加入到敌方数组
  enemyarry.push(newEnemy1);
  enemyarry.push(newEnemy2);
  enemyarry.push(newEnemy3);
  enemyarry.push(newEnemy4);
  enemyarry.push(newEnemy5);
}
setInterval(function () {
  getEnemy();
}, 6000);

//----------------------------------------------------------------------
//添加星星
function xx(x, y) {
  const xx = document.createElement("i");
  xx.innerHTML = "&#xe616;";
  xx.className = "iconfont";
  xx.style.position = "absolute";
  xx.style.left = x + "px";
  xx.style.top = y + "px";
  xx.style.color = col();
  main.appendChild(xx);
  return xx;
}
//随机生成星星
function getxx() {
  xing1 = xx(getRandom(0, 3000), getRandom(0, 1400));
  xingarry.push(xing1);
}
//出现星星
setInterval(function () {
  getxx();
}, 300);

//-------------------------------------------------------------------------------
//随机颜色函数
function col() {
  var r = parseInt(Math.random() * 256);
  var g = parseInt(Math.random() * 256);
  var b = parseInt(Math.random() * 256);
  return "rgb(" + r + "," + g + "," + b + ")";
}

//生成篮球---------------------------------------------------------------
function lq(x, y) {
  const lq = document.createElement("i");
  lq.innerHTML = "&#xea73;";
  lq.className = "iconfont";
  lq.style.fontSize = 30 + "px";
  lq.style.position = "absolute";
  lq.style.left = x + "px";
  lq.style.top = y + "px";
  lq.style.color = "#FF4500";
  main.appendChild(lq);
  return lq;
}
//随机生成篮球
function getlq() {
  qiu1 = lq(getRandom(0, 3000), getRandom(0, 1400));
  qiuarry.push(qiu1);
}
//出现篮球
setInterval(function () {
  getlq();
}, 5000);

//添加金色切尔西------------------------------------------------------------
function xie(x, y) {
  const xie = document.createElement("i");
  xie.style.fontSize = 40 + "px";
  xie.innerHTML = "&#xe600;";
  xie.className = "iconfont";
  xie.style.position = "absolute";
  xie.style.left = x + "px";
  xie.style.top = y + "px";
  xie.style.color = "#FFD700";
  main.appendChild(xie);
  return xie;
}
//随机生成切尔西
function getxie() {
  qex = xie(getRandom(0, 3000), getRandom(0, 1400));
  qexarry.push(qex);
}
//出现切尔西
setInterval(function () {
  getxie();
}, 8000);
//出现流星-----------------------------------------------------------
function ji(x, y, spead, fangxiang) {
  const ji = document.createElement("i");
  ji.innerHTML = "&#xe714;";
  ji.className = "iconfont chick";
  ji.style.position = "absolute";
  ji.style.left = x + "px";
  ji.style.top = y + "px";
  ji.style.fontSize = 30 + "px";
  // setInterval(() => {
  // }, 200);
  ji.spead = spead + "px";
  fangxiang = spead * Math.cos(fangxiang);
  ji.fangxaing = fangxiang;
  ji.style.color = "#000000";
  main.appendChild(ji);
  return ji;
}
//随机生成流星
function getji() {
  jirou = ji(getRandom(0, 3000), getRandom(0, 1400), 20, getRandom(0, 150));
  jiarry.push(jirou);
}
var posX;
var posY;
//流星出出现
var a = setInterval(function () {
  getji();
  chicks = document.querySelectorAll(".chick");
  chicks.forEach((item) => {
    // let timer = setInterval(() => {
    posX = item.offsetLeft;
    posY = item.offsetTop;
    posX += 20;
    posY += 20;
    for (let i = 0; i < jiarry.length; i++) {
      if (posX > 3000 || posY > 1400) {
        main.removeChild(jiarry[i]);
        jiarry.splice(i, 1);
      }
    }
    item.style.left = posX + "px";
    item.style.top = posY + "px";
  });
}, 300);
//-------------------------------------------------------------------------------
//随机颜色函数
function col() {
  var r = parseInt(Math.random() * 256);
  var g = parseInt(Math.random() * 256);
  var b = parseInt(Math.random() * 256);
  return "rgb(" + r + "," + g + "," + b + ")";
}
//-------------------------------------------------------------------
//获取鼠标位置
main.onmousemove = function (e) {
  // console.log(13);
  //检测碰撞
  peng2(e);
  peng1(e);
  peng3(e);
  peng4(e);
  peng5(e);
};
//碰撞小星星获得积分---------------------------------------------
function peng1(e) {
  // var xings = document.querySelectorAll(".");
  for (let i = 0; i < xingarry.length; i++) {
    //拿到星星X和Y坐标
    let xingTop = xingarry[i].style.top;
    let xingLeft = xingarry[i].style.left;
    if (
      xingTop >= e.clientY - 30 + "px" &&
      xingTop <= e.clientY + 30 + "px" &&
      xingLeft >= e.clientX - 30 + "px" &&
      xingLeft <= e.clientX + 40 + "px"
    ) {
      //判定生效移出星星
      // console.log(e.clientX + "px" + "5555555555555555555555");
      main.removeChild(xingarry[i]);
      xingarry.splice(i, 1);
      //分数加一
      score2++;
    }
  }
  elmscore2.innerHTML = score2;
}
//碰撞IKUN，减少积分-----------------------------------------------------
function peng2(e) {
  for (let j = 0; j < enemyarry.length; j++) {
    //拿到假IKUN的X和Y坐标
    let enTop = parseInt(enemyarry[j].imgNode.style.top);
    let enLeft = parseInt(enemyarry[j].imgNode.style.left);
    // console.log(enTop + "55555555555555");
    if (
      enTop >= e.clientY - 20 &&
      enTop <= e.clientY + 20 &&
      enLeft >= e.clientX - 20 &&
      enLeft <= e.clientX + 20
    ) {
      //判定生效移出假IKUN
      main.removeChild(enemyarry[j].imgNode);
      enemyarry.splice(j, 1);
      //分数加一
      score2 = score2 - 3;
    }
  }
  elmscore2.innerHTML = score2;
}
//碰撞篮球获得积分----------------------------------------
function peng3(e) {
  // var xings = document.querySelectorAll(".");
  for (let i = 0; i < qiuarry.length; i++) {
    //拿到星星X和Y坐标
    let qiuTop = qiuarry[i].style.top;
    let qiuLeft = qiuarry[i].style.left;
    // console.log(xingTop + "5555555555555555555555");
    // console.log(xingLeft + "5555555555555555555555");
    // console.log(e.clientY + "px");
    //console.log(e.clientX + "5555555555555555555555");
    if (
      qiuTop >= e.clientY - 30 + "px" &&
      qiuTop <= e.clientY + 30 + "px" &&
      qiuLeft >= e.clientX - 30 + "px" &&
      qiuLeft <= e.clientX + 40 + "px"
    ) {
      //判定生效移出星星
      // console.log(e.clientX + "px" + "5555555555555555555555");
      main.removeChild(qiuarry[i]);
      qiuarry.splice(i, 1);
      //分数加一
      score1++;
    }
  }
  elmscore1.innerHTML = score1;
}
//碰到金色切尔西--------------------------------------------------------
function peng4(e) {

  // var xings = document.querySelectorAll(".");
  for (let i = 0; i < qexarry.length; i++) {
    //拿到星星X和Y坐标
    let xieTop = qexarry[i].style.top;
    let xieLeft = qexarry[i].style.left;
  
    if (
      xieTop >= e.clientY - 30 + "px" &&
      xieTop <= e.clientY + 30 + "px" &&
      xieLeft >= e.clientX - 30 + "px" &&
      xieLeft <= e.clientX + 40 + "px"
    ) {
      main.removeChild(qexarry[i]);
      qiuarry.splice(i, 1);
      //分数--
      score1--;
    }
  }
  elmscore1.innerHTML = score1;
}
//检测与星星碰撞----------------------------------------------------
function peng5(e) {
  // var xings = document.querySelectorAll(".");
  for (let i = 0; i < jiarry.length; i++) {
    //拿到星星X和Y坐标
    let jiTop = jiarry[i].style.top;
    let jiLeft = jiarry[i].style.left;
    if (
      jiTop >= e.clientY - 30 + "px" &&
      jiTop <= e.clientY + 30 + "px" &&
      jiLeft >= e.clientX - 30 + "px" &&
      jiLeft <= e.clientX + 40 + "px"
    ) {
      //判定生效移出星星
      main.removeChild(jiarry[i]);
      jiarry.splice(i, 1);
      //分数加一
      score2 = score2 - 3;
    }
  }
  elmscore2.innerHTML = score2;
}
//游戏结束与重新开始----------------------------------------------------------------------------
setInterval(function () {
  if (score1 < 0 || score2 < 0) {
    //重新开始
    location = location;
  }
  if (score1 > 5) {
    window.location.href =
      "为爱冲锋.html";
  }
  if (score2 > 100) {
    window.location.href =
      "坤龙.html";
  }
 document.cookie=timer;
}, 100);
//定义计时器-------------------------------------------------------------------------------------
let timer = 0;
  setInterval(() => {
    timer++;
    document.getElementById("time").innerText = timer;
  }, 1000);
};